package com.fun.feifun.modules.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fun.feifun.base.utils.common.ListUtil;
import com.fun.feifun.modules.business.model.BusinessGoods;
import com.fun.feifun.modules.business.model.BusinessGoodsSupplierSort;
import com.fun.feifun.modules.business.model.BusinessSupplierGoods;
import com.fun.feifun.modules.business.dao.BusinessSupplierGoodsMapper;
import com.fun.feifun.modules.business.model.BusinessUserGoods;
import com.fun.feifun.modules.business.model.dto.BusinessSupplierGoodsDto;
import com.fun.feifun.modules.business.model.dto.BusinessUserGoodsDto;
import com.fun.feifun.modules.business.model.param.*;
import com.fun.feifun.modules.business.service.IBusinessGoodsService;
import com.fun.feifun.modules.business.service.IBusinessSupplierGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务用户商品信息 服务实现类
 * </p>
 *
 * @author wkf
 * @since 2024-06-06
 */
@Service
public class BusinessSupplierGoodsServiceImpl extends ServiceImpl<BusinessSupplierGoodsMapper, BusinessSupplierGoods> implements IBusinessSupplierGoodsService {

    @Resource
    private IBusinessGoodsService goodsService;

    @Override
    public List<BusinessSupplierGoodsDto> getSupplierDtoPage(BusinessSupplierParam page) {
        List<BusinessSupplierGoodsDto> list = baseMapper.selectDtoList(page);
        return list;
    }

    @Override
    public Boolean saveSupplierGoods(BusinessSupplierGoodsParam param) {
        List<BusinessSupplierGoods> list=new ArrayList<>();
        Long userId = param.getUserId();
        List<BusinessSupplierGoodsParam.GoodsFees> goodsList = param.getGoodsList();
        //获取商品
        List<Long> goodsIds = goodsList.stream().map(it -> it.getGoodsId()).collect(Collectors.toList());
        QueryWrapper<BusinessGoods> goodsWrapper=new QueryWrapper<>();
        goodsWrapper.lambda().in(BusinessGoods::getId,goodsIds);
        List<BusinessGoods> goodss = goodsService.list(goodsWrapper);
        Map<Long, BusinessGoods> goodsMap =new HashMap<>();
        if(!ListUtil.isEmpty(goodss)){
            goodsMap=goodsService.list(goodsWrapper).stream().collect(Collectors.toMap(BusinessGoods::getId, goods -> goods));
        }
        //组装商品用户关联对象。
        for (BusinessSupplierGoodsParam.GoodsFees goodsFees:goodsList) {
            BusinessSupplierGoods supplierGoods =new BusinessSupplierGoods();
            supplierGoods.setId(goodsFees.getId());
            supplierGoods.setSupplierId(userId);
            supplierGoods.setGoodsId(goodsFees.getGoodsId());
            supplierGoods.setFeesAmount(goodsFees.getFeesAmount());
            supplierGoods.setProductCode(goodsFees.getProductCode());
            supplierGoods.setHasAuthor(goodsFees.isHasAuthor());
            BusinessGoods goods = goodsMap.get(goodsFees.getGoodsId());
            if(ObjectUtils.isEmpty(goods)){
                supplierGoods.setTotalAmount(goods.getAmount().add(goodsFees.getFeesAmount()));
                supplierGoods.setGoodsAmount(goods.getAmount());
            }
            list.add(supplierGoods);
        }
        return saveOrUpdateBatch(list);
    }


}
